/* ===================

AX2E (AXESCON XACML 2.0 Engine) is the Java authorization engine, which implements OASIS XACML 2.0 standard.
Copyright (C) 2007 AXESCON LLC

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA


Contact: AXESCON LLC - info{at}axescon.com

 =================== */
package axescon.xacml.samples.rbac_2_0;

import axescon.xacml.api.XacmlException;
import axescon.xacml.test.LoopLoadTest;
import axescon.xacml.util.EngineConfigurator;
import axescon.xacml.util.FilePolicyStore;
import axescon.xacml.util.IdRefOnlyFilePolicyStore;

import java.io.File;

/**
 * @author Date: Sep 19, 2005
 *         Responsibilities: demonstrate how to add a custom function to AXESCON XACML Engine.
 */
public class Rbac2Test extends LoopLoadTest {

    static {
        testSute = new Rbac2Test();
    }

    /**
     * this method sets up policy stores for XACML RBAC Profile 2.0 tests.
     * these tests have one set of policies for all tests. there's two types of policies:
     * - PPS, which are looked up only by reference
     * - RPS, which are looked up by target matching
     * <p/>
     * See the details in RBAC profile specification.
     *
     * @param conf
     * @param dir
     * @param testName
     * @throws XacmlException
     */
    protected void setPolicyStores(EngineConfigurator conf, File dir, String testName) throws XacmlException {
        // configure policy store
        File[] pfs = dir.listFiles(new PolicyFilter("")); // grab all policies, no need for test name
        FilePolicyStore ps = new FilePolicyStore(); // policy store, looks up policies by target matching
        ps.setPolicyFactory(conf.getPolicyFactory()); // set the XmlBeans based factory

        IdRefOnlyFilePolicyStore idfps = null;
        for (int j = 0; j < pfs.length; j++) {
            File pf = pfs[j];
            String fn = pf.getName();
            if (fn.indexOf("pps-") >= 0) {
                // handle policies to be looked up by ref only, i.e. PPS for RBAC
                // this will set policy ref store, when CT needs it
                if (idfps == null) {
                    idfps = new IdRefOnlyFilePolicyStore();
                    idfps.setPolicyFactory(conf.getPolicyFactory());
                }
                idfps.addPolicy(pf);
            } else { // standard policy store for RBAC RPS policies
                ps.addPolicy(pf);
            }
        }

        conf.getPolicyEvaluator().setPolicyReferenceStore(idfps); // set engine's policy reference store

        conf.getPolicyEvaluator().setPolicyStore(ps); // now the engine has policy store

    }

    /*
    public void handlePDPException(String testName, XacmlException t) throws XacmlException {
        if (testName.endsWith(File.separator + "rbac05") &&
                t.getErrCode()==XacmlException.ERR_CODE_PROCESSING_NO_MATCHING_POLICIES) {
            // this test has no matching policy
            System.out.print("*");
            passed++;
        } else
            throw t;
    }
    */

}
